//
//  HomeViewController.swift
//  新浪微博
//
//  Created by yaozhen on 2017/11/27.
//  Copyright © 2017年 ZYZ. All rights reserved.
//

import UIKit

//定义全局常量  尽量使用private 修饰 否则导出都可以访问
private let cellID = "cellID"
class HomeViewController: BaseViewController {
    
    private lazy var listViewModel = StatusListViewModel()
    

    override func viewDidLoad() {
        super.viewDidLoad()
        if NetworkManager.shared.userLogOn {
            settingNavigationBar()
        }
        
    }
    override func loadData() {
        print("准备刷新，最后一条\(String(describing: self.listViewModel.statusList.last?.text))")
        listViewModel.loadStatus(pullup: self.isPullup) { (_ isSuccess: Bool, _ shouldRefresh:Bool) in
            
            print("加载数据结束")
            //结束刷新控件
            self.refreshControl?.endRefreshing()
            //恢复上拉刷新标记
            self.isPullup = false
            if shouldRefresh{
               self.tableView?.reloadData()
            }
            
        }
    }
    @objc private func showFriends(){
        print("添加\(#function)")
        let vc = DemoViewController()
//        vc.hidesBottomBarWhenPushed = true
        //push 的动作是nav 做的
        
        navigationController?.pushViewController(vc, animated: true)
    }
}
//MARK -- 表格数据源方法
extension HomeViewController {
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listViewModel.statusList.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell.init(style: .default, reuseIdentifier: cellID)
        cell.textLabel?.text = listViewModel.statusList[indexPath.row].text
        return cell
    }
}
extension HomeViewController  {
    private func settingNavigationBar() {
        //设置导航条按钮
        navIem.leftBarButtonItem = UIBarButtonItem.init(title: "好友", fontSize: 16, target: self, action: #selector(showFriends))
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellID)
    }
    
}
